﻿using System;

namespace Encloser
{
    public static class TimeUtils
    {
        private static readonly DateTime ZERO = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));

        public static string ToDateString(this DateTime time)
        {
            return time.ToString("yyyy-MM-dd");
        }

        public static string ToDateTimeString(this DateTime time, bool showMilliSecond = false)
        {
            return showMilliSecond ? time.ToString("yyyy/MM/dd HH:mm:ss:fff") : time.ToString("yyyy/MM/dd HH:mm:ss");
        }

        public static DateTime ToDateTime(this int unix)
        {
            return ZERO.AddSeconds(unix);
        }

        public static DateTime ToDateTime(this long unix)
        {
            return ZERO.AddMilliseconds(unix);
        }

        public static int ToIntUnix(this DateTime dateTime)
        {
            return (int)Math.Round((dateTime - ZERO).TotalSeconds);
        }

        public static long ToLongUnix(this DateTime dateTime)
        {
            return (long)Math.Round((dateTime - ZERO).TotalMilliseconds);
        }
    }
}
